package com.dsg.controller.oil;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsg.common.constants.MessageConstants;
import com.dsg.common.dto.AjaxResult;
import com.dsg.common.util.SingletonLoginUtils;
import com.dsg.common.util.toolbox.StringUtil;
import com.dsg.controller.base.BaseController;
import com.dsg.entity.oil.DsgOilCardStock;
import com.dsg.service.oil.IDsgOilCardStockService;
import com.dsg.util.ExcelIteratorReader;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 油咭庫存 前端控制器
 * </p>
 *
 * @author system
 * @since 2020-07-28
 */
@RestController
@RequestMapping("/oil/dsgOilCardStock")
public class DsgOilCardStockController extends BaseController {

    @Autowired
    private IDsgOilCardStockService service;

    @GetMapping(value = "/returnCardSC")
    @ApiOperation("SC返咭類型")
    @ApiResponses(
            @ApiResponse(response = DsgOilCardStock.class, code = 0, message = "ok")
    )
    public AjaxResult returnCardSC(String cardNo) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("STOCK_STATUS", DsgOilCardStock.STOCK_STATUS_IMPORT);
        wrapper.eq("CARD_NO", cardNo);
        wrapper.eq("ENT_CODE", getEntCode());
        DsgOilCardStock stock = service.getOne(wrapper);
        return success(stock);
    }

    @PostMapping(value = "/returnCardConfirm")
    @ApiOperation("SC返咭-確認")
    public AjaxResult returnCardConfirm(@RequestBody List<DsgOilCardStock> list) {
        list.forEach(e -> e.setStockStatus(DsgOilCardStock.STOCK_STATUS_CONFIRM));
        service.updateBatchById(list);
        return success();
    }

    @ApiOperation("SC返咭-上载Excel")
    @PostMapping(value = "/importExcel")
    public AjaxResult importExcel(@ApiParam(value = "file", name = "file", required = true) @RequestParam("file") MultipartFile file,
                                  @ApiParam(value = "1-補咭，2-新咭，3-Renew咭", name = "returnType", required = true) @RequestParam Integer returnType) throws Exception {
        File excelFile = ExcelIteratorReader.multipartToFile(file);
        return service.importExcel(excelFile, returnType);
    }

    /**
     * 查询列表（分页）
     *
     * @param dsgOilCardStock
     * @return
     */
    @GetMapping(value = "/list")
    @ApiOperation("查询列表（分页）")
    public AjaxResult list(DsgOilCardStock dsgOilCardStock) {
        // 分页
        Page page = getPage();
        String sort = getParameter("sort");
        List<OrderItem> orderItemList = new ArrayList<>();
        OrderItem orderItem = new OrderItem();
        if (StringUtil.isEmpty(sort)) {
            orderItem.setColumn("CREATE_TIME");
            orderItem.setAsc(false);
            orderItemList.add(orderItem);
            page.setOrders(orderItemList);
        }
        dsgOilCardStock.setEntCode(SingletonLoginUtils.getSystemEntCode());
        page = service.selectByList(page, dsgOilCardStock);
        // 格式要返回的数据
        AjaxResult ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, page.getRecords(), (int) page.getTotal());
        return ajaxResult;
    }
}
